import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/login/login.vue'
import Layout from '../views/layout/layout.vue'
import Home from '../views/home/home.vue'
import Article from '../views/articleManage/articleManage.vue'
import Material from '../views/materialManage/materialManage.vue'
import Publish from '../views/publish/publish.vue'
import Comment from '../views/comment/comment.vue'
import Fans from '../views/fans/fans.vue'
import Settings from '../views/settings/settings.vue'

Vue.use(VueRouter)

const routes = [
  {path : "/",redirect :'/login'},
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/home',
    component: Layout,
    children:[
      {
        path: '',
        name: 'home',
        component: Home
      },
      {
        path: '/article',
        name: 'article',
        component: Article
      },
      {
        path: '/material',
        name: 'material',
        component: Material
      },
      {
        path: '/publish',
        name: 'publish',
        component: Publish
      },
      {
        path: '/comment',
        name: 'comment',
        component: Comment
      },
      {
        path: '/fans',
        name: 'fans',
        component: Fans
      },
      {
        path: '/settings',
        name: 'settings',
        component: Settings
      },
    ]

  },

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  //如果导航不是到登入页面
  if(to.name !== 'login') {
      //如果本地有数据，就放行
      if(window.localStorage.getItem('user')){
        next()
      }else{
        alert("登入已经退出，请重新登入")
        //没有数据就跳转到登入页面
        next('/login')
      }
  }else{
      //如果导航到登入页面就放行
      next()
  }

})

export default router
